<?php
declare (strict_types = 1);
namespace app;
use think\App;
use think\exception\FuncNotFoundException;
use think\exception\ValidateException;

/**
 * 控制器基础类
 */
abstract class Base
{
    /**
     * Request实例
     * @var \think\Request
     */
    protected $request;
	
	protected $status;

    /**
     * 应用实例
     * @var \think\App
     */
    protected $app;

    /**
     * 控制器中间件
     * @var array
     */
    protected $middleware = [];

    /**
     * 构造方法
     * @access public
     * @param  App  $app  应用对象
     */
    public function __construct(App $app)
    {
        $this->app     = $app;
        $this->request = $this->app->request;
		$this->status=array('code'=>200,'msg'=>'','data'=>array());
		//登录控制
		if($this->request->controller()!='Login')
		{
			if(!$this->request->admin['userid'])
			{
				abort(403, $this->request->controller());
			}
		}
		
		//abort(403, '测试');
        // 控制器初始化
        $this->initialize();
		
		
    }

    // 初始化
    protected function initialize()
    {
		
	}

    protected function kerror($str)
	{
		$this->status['code']=0;
		$this->status['msg']=$str;
		return json($this->status);
		
	}
	
	protected function ok(){
		return json($this->status);
	}
	
	protected function setobj($obj)
	{
		$this->status['data']=$obj;
	}
	
	protected function setarr($key,$obj)
	{
		$this->status['data'][$key]=$obj;
	}
	
	protected function validate($data,$validate){
		try{
			validate($validate)->scene($this->request->action())->check($data);
		}catch(ValidateException $e){
			return $e->getError();
		}
		return false;
	}

}
